from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from ..utils.msg_websocket import manager


router = APIRouter()

@router.websocket("/msg/{client_id}")
async def websocket_endpoint(client_id:str, websocket: WebSocket):
    await manager.connect(client_id, websocket)
    try:
        while True:
            # 接收客户端发送的消息
            data = await websocket.receive_text()
            await manager.send_text(data)
    except WebSocketDisconnect:
        manager.disconnect(client_id, websocket)